package com.zsy.controller;

import com.zsy.annoation.ReportLogAnno;
import com.zsy.pojo.Emp;
import com.zsy.pojo.EmpQueryParam;
import com.zsy.pojo.PageResult;
import com.zsy.pojo.Result;
import com.zsy.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: zhengsuyang
 * @Desc:
 * @create: 2025-08-02 19:00
 **/
@RestController
@Slf4j
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    /**
     * 分页查询获取员工列表
     *
     * @return
     */
/*    @GetMapping()
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询：{},{}", page, pageSize);
        PageResult<Emp> pageResult = empService.page(page, pageSize);
        return Result.success(pageResult);
    }*/
    @GetMapping()
    public Result page(EmpQueryParam empQueryParam) {
        log.info("分页查询：{}", empQueryParam);
        PageResult<Emp> pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectEmpById(@PathVariable Integer id){
        log.info("进入根据id去查询员工{}",id);
      Emp emp = empService.selectEmpById(id);
      return Result.success(emp);
    }

    /**
     * 查询全部员工
     * @return
     */
    @GetMapping("/list")
    public Result queryEmpList(){
       List<Emp> emps = empService.queryEmpList();
       return Result.success(emps);
    }

    /**
     * 新增员工
     * @param emp
     * @return
     */
    @PostMapping
    @ReportLogAnno
    public Result addEmp(@RequestBody Emp emp){
        log.info("请求参数emp: {}", emp);
        empService.addEmp(emp);
        return Result.success();
    }

    /**
     * 修改员工
     * @param emp
     * @return
     */
    @PutMapping
    @ReportLogAnno
    public Result updateEmp(@RequestBody Emp emp){
        log.info("请求参数emp: {}", emp);
        empService.updateEmp(emp);
        return Result.success();
    }
    /**
     * 批量删除员工
     * @param ids
     * @return
     */
    @DeleteMapping
    @ReportLogAnno
    public Result deleteByIds(@RequestParam("ids") List<Integer> ids){
        log.info("删除员工：id{}", ids);
        empService.deleteById(ids);
        return Result.success();
    }
}
